package com.ziq.zcamp.util

import android.app.Activity
import android.graphics.Color
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener

class PictureUtil {
    companion object {
        fun selectVideo(activity: Activity, selectionData : List<LocalMedia>, callback : OnResultCallbackListener<LocalMedia>,
                        maxSelectNum:Int = 1, isCamera:Boolean = true){
            PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofVideo())
                .imageEngine(GlideEngine.createGlideEngine())
                .selectionData(selectionData)
                .isMaxSelectEnabledMask(true)// 选择数到了最大阀值列表是否启用蒙层效果
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                .isPreviewVideo(true)
                .isCamera(isCamera)// 是否显示拍照按钮
                .maxSelectNum(maxSelectNum)
                .maxVideoSelectNum(maxSelectNum)
                .minVideoSelectNum(1)
                .isCompress(true)// 是否压缩
                .videoQuality(100)
                .isOpenStyleCheckNumMode(true)
                .forResult(callback)
        }

        fun selectPicture(activity: Activity,  selectionData : List<LocalMedia>, callback : OnResultCallbackListener<LocalMedia>,
                          maxSelectNum:Int = 18, isCamera:Boolean = true,needGif : Boolean = true){
            PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .isGif(needGif)
                .imageEngine(GlideEngine.createGlideEngine())
                .selectionData(selectionData)
                .isMaxSelectEnabledMask(true)// 选择数到了最大阀值列表是否启用蒙层效果
                .maxSelectNum(maxSelectNum)// 最大图片选择数量
                .minSelectNum(1)// 最小选择数量
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                .isPreviewImage(true)// 是否可预览图片
                .isCamera(isCamera)// 是否显示拍照按钮
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .minimumCompressSize(5000)// 5m 以下不压缩
                .isCompress(true)// 是否压缩
                .compressQuality(100)// 图片压缩后输出质量 0~ 100
                .isOpenStyleCheckNumMode(true)
                .compressSavePath(StorageUtil.cropPath)
                .forResult(callback)
        }

        fun selectPictureAndCrop(activity: Activity, callback : OnResultCallbackListener<LocalMedia>, aspect_ratio_x:Int = 1, aspect_ratio_y: Int = 1){
            PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                .isPreviewImage(false)// 是否可预览图片
                .isCamera(true)// 是否显示拍照按钮
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .compressSavePath(StorageUtil.cropPath)
                .isEnableCrop(true)
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .cutOutQuality(90)// 裁剪输出质量 默认100
                .minimumCompressSize(500)// 小于多少kb的图片不压缩
                .withAspectRatio(aspect_ratio_x, aspect_ratio_y)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                .rotateEnabled(false)// 裁剪是否可旋转图片
                .setCircleDimmedColor(Color.parseColor("#96000000"))
                .setCropStatusBarColorPrimaryDark(Color.parseColor("#000000"))
                .forResult(callback)
        }

        fun getPath(media : LocalMedia) : String {
            return if (media.isCut && !media.isCompressed) {
                // 裁剪过
                media.cutPath?:""
            } else if (media.isCompressed || media.isCut && media.isCompressed) {
                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                media.compressPath?:""
            } else {
                // 原图
                media.realPath?:""
            }
        }

        fun calculatePostingDetailPictureViewHeight(itemWidth:Int, pictureWidth:Int?, pictureHeight:Int?): Int{
            var result = 0
            if(pictureWidth != null && pictureWidth > 0
                && pictureHeight != null && pictureHeight > 0){
                val ratio = pictureWidth.toFloat() / pictureHeight.toFloat()
                if(ratio > 5f / 2f){
                    result = (itemWidth.toFloat() * 2f / 5f).toInt()
                }else if(ratio < 5f / 7f){
                    result = (itemWidth.toFloat() * 7f / 5f).toInt()
                }else {
                    result = (itemWidth.toFloat() * pictureHeight.toFloat() / pictureWidth.toFloat()).toInt()
                }
            } else {
                result = (itemWidth.toFloat() * 7f / 5f).toInt()
            }
            return result
        }

        fun calculatePictureViewHeight(itemWidth:Int, pictureWidth:Int?, pictureHeight:Int?): Int{
            var result = 0
            if(pictureWidth != null && pictureWidth > 0
                && pictureHeight != null && pictureHeight > 0){
                val ratio = pictureWidth.toFloat() / pictureHeight.toFloat()
                if(ratio > 5f / 2f){
                    result = (itemWidth.toFloat() * 2f / 5f).toInt()
                }else if(ratio < 5f / 7f){
                    result = (itemWidth.toFloat() * 7f / 5f).toInt()
                }else {
                    result = (itemWidth.toFloat() * pictureHeight.toFloat() / pictureWidth.toFloat()).toInt()
                }
            } else {
                result = (itemWidth.toFloat() * 7f / 5f).toInt()
            }
            return result
        }

        fun calculatePlacePictureHeight(itemWidth:Int, pictureWidth:Int?, pictureHeight:Int?): Int{
            var result = 0
            if(pictureWidth != null && pictureWidth > 0
                && pictureHeight != null && pictureHeight > 0){
                val ratio = pictureWidth.toFloat() / pictureHeight.toFloat()
                if(ratio > 8f / 5f){
                    result = (itemWidth.toFloat() * 5f / 8f).toInt()
                }else if(ratio < 1f / 1f){
                    result = (itemWidth.toFloat() * 1f / 1f).toInt()
                }else {
                    result = (itemWidth.toFloat() * pictureHeight.toFloat() / pictureWidth.toFloat()).toInt()
                }
            } else {
                result = (itemWidth.toFloat() * 1f / 1f).toInt()
            }
            return result
        }
    }
}